<template>
  <el-aside :width="main.isCollapse? '180px': '64px'">

    <el-menu :collapse-transition="false" :collapse="!main.isCollapse" default-active="2" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff">
      <p v-show="main.isCollapse">VUE后台</p>
      <p class="aside-title" v-show="!main.isCollapse">VUE</p>
      <el-menu-item @click="clickMenu(item)" v-for="item in noChildren()" :key="item.path" :index="item.path">
       <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>

      <el-sub-menu v-for="item in hasChildren()" :key="item.path"  :index="item.label">
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group >
          <el-menu-item v-for="(subItem,subIndex) in item.children" :index="subItem.path" :key="subIndex">
            
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>


    </el-menu>

  </el-aside>
</template>

<script setup>
// 引入仓库
import { useAside } from '@/store/index.js';
// 引用router
import { useRouter } from 'vue-router';
const router =useRouter()

const main=useAside()

const list = [
  {
    path: '/',
    name: 'home',
    label: '首页',
    icon: 'house',
    url: 'Home/Home'
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'MallManage/MallManage'
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'UserManage/UserManage'
  },
  {
    label: '其他',
    icon: 'location',
    path: '/outher',
    children: [
      {
        path: '/page1',
        name: 'page1',
        label: '页面1',
        icon: 'setting',
        url: 'Other/PageOne'
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'Other/PageTwo'
      }
    ]
  }
]

const handleSelectMenu =(val)=>{
  main.selectMenu(val)
}

// 点击菜单栏
const clickMenu =(item)=>{
  router.push({
    name:item.name
  })
  handleSelectMenu(item)
}


const noChildren = () => {
  return list.filter((item) => !item.children)
}
const hasChildren = () => {
  return list.filter((item) => item.children)
}


</script>


<style lang="less">
.icons{
  width: 16px;
  height: 16px;
}
.el-menu{
  border-right: none;
  p{
    font-weight: 700;
    line-height: 45px;
    color: #fff;
    text-align: center;
  }

}


</style>